#!/usr/bin/env python
# encoding: utf-8
"""
#-------------------------------------------------------------------#
#                   CONFIDENTIAL --- CUSTOM STUDIOS                 #     
#-------------------------------------------------------------------#
#                                                                   #
#                   @Project Name : Globallawonline                #
#                                                                   #
#                   @File Name    : change.py                      #
#                                                                   #
#                   @Programmer   : 李建                            #
#                                                                   #  
#                   @Start Date   : 2021/4/19 14:41                 #
#                                                                   #
#                   @Last Update  : 2021/4/19 14:41                 #
#                                                                   #
#-------------------------------------------------------------------#
# Classes:修改新闻的html富文本数据                                                          #
#                                                                   #
#-------------------------------------------------------------------#
"""
# 这是mysql数据库的连接属性
import re

import pymysql

MYSQL_CONNECT = {
    'MYSQL_HOST': '114.115.219.219',
    'MYSQL_PORT': 3306,
    'MYSQL_DB': 'globallegaldatabase',
    'MYSQL_USER': 'root',
    'MYSQL_PASSWD': 'xinanzhengfadaqxueapp03!',
    'MYSQL_CHARSET': 'utf8',
    'MYSQL_TABLE': 'lawnews'  # 这是数据库存储数据的表
}

sqlcon = pymysql.connect(host=MYSQL_CONNECT['MYSQL_HOST'],
                         port=MYSQL_CONNECT['MYSQL_PORT'],
                         db=MYSQL_CONNECT['MYSQL_DB'],
                         user=MYSQL_CONNECT['MYSQL_USER'],
                         passwd=MYSQL_CONNECT['MYSQL_PASSWD'], charset='utf8')
sqlcur = sqlcon.cursor()
sql = '''select ID,Fulltexthtml FROM lawnews WHERE SortA = "LAWCOUNTRYTG"'''
try:
    sqlcur.execute(sql)
    trs = sqlcur.fetchall()
    for tr in trs:
        html = tr[1]
        new_html1 = re.sub('''href="http://www.led.go.th/Actnews//Actnews/''', '''href="http://www.led.go.th/Actnews/''', html, re.I)
        new_html = re.sub('''src="http://www.led.go.th/Actnews//Actnews/''', '''src="http://www.led.go.th/Actnews/''', new_html1, re.I)
        new_html = pymysql.escape_string(new_html)
        upsql = '''UPDATE lawnews SET Fulltexthtml = "%s" WHERE ID = "%s"''' % (new_html, tr[0])
        try:
            sqlcur.execute(upsql)
            sqlcon.commit()
        except Exception as e:
            print("法律新闻更新出错:%s" % str(e))
        print(new_html)
except Exception as e:
    print(str(e))